<?php

namespace app\api\lib;

use app\common\service\IpService;
use DateTime;

class SuPengJun
{

    public static function timeStr($time)
    {
        if (!$time) return '未知';
        $time = strtotime($time);
        //将日期转化为时间戳
        $etime = time() - $time;
        switch ($etime) {
            case $etime <= 60:
                $result = '刚刚';
                break;
            case $etime > 60 && $etime <= 60 * 60:
                $result = floor($etime / 60) . '分钟前';
                break;
            case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
                $result = date('Ymd', $time) == date('Ymd', time()) ? '今天 ' . date('H:i', $time) : '昨天 ' . date('H:i', $time);
                break;
            case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
                $result = date('Ymd', $time) + 1 == date('Ymd', time()) ? '昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time);
                break;
            case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
                $result = date('Y', $time) == date('Y', time()) ? date('m-d', $time) : date('Y-m-d', $time);
                break;
            default:
                $result = date('Y-m-d', $time);
        }
        return $result;
    }

    public static function formatTimeStamps($timestamp1, $timestamp2)
    {
        $weekFormat = '周';
        $timeFormat = 'H:i';
        $weekdays = array('日', '一', '二', '三', '四', '五', '六');
        $weekdayIndex1 = date('w', $timestamp1);
        $weekdayIndex2 = date('w', $timestamp2);
        $chineseWeekday1 = $weekdays[$weekdayIndex1];
        $chineseWeekday2 = $weekdays[$weekdayIndex2];
        $date1 = date('Y.m.d', $timestamp1);
        $date2 = date('Y.m.d', $timestamp2);
        if ($date1 == $date2) {
            $output = date($timeFormat, $timestamp1) . '-' . date($timeFormat, $timestamp2) . ' ' . $weekFormat . $chineseWeekday1 . ' ' . date('m.d', $timestamp1);
        } else {
            $output = $weekFormat . $chineseWeekday1 . ' ' . date('m.d', $timestamp1) . ' ' . date($timeFormat, $timestamp1) . ' - ' . $weekFormat . $chineseWeekday2 . ' ' . date('m.d', $timestamp2) . ' ' . date($timeFormat, $timestamp2);
            if (date('Y', $timestamp1) != date('Y', $timestamp2)) {
                $output = $weekFormat . $chineseWeekday1 . ' ' . date('Y.m.d', $timestamp1) . ' ' . date($timeFormat, $timestamp1) . ' - ' . $weekFormat . $chineseWeekday2 . ' ' . date('Y.m.d', $timestamp2) . ' ' . date($timeFormat, $timestamp2);
            }
        }
        return $output;
    }


    public static function ipStr($model, $ip)
    {
        $result = (new IpService)->simpleJson($ip);
        $model['ip'] = $ip;
        $model['country'] = $result[0];
        $model['province'] = $result[2];
        $model['city'] = $result[3];
        return $model;
    }

    public static function addsStr($text, $type = 0)
    {
        if (!$text && $type == 0) return 'IP属地：未知';
        if (!$text && $type == 1) return '';
        $text = str_replace('省', '', $text);
        $text = str_replace('市', '', $text);
        $text = str_replace('壮族自治区', '', $text);
        $text = str_replace('回族', '', $text);
        $text = str_replace('自治区', '', $text);
        $text = str_replace('特别行政区', '', $text);
        return $text;
    }

    public static function mobileStr($mobile)
    {
        if ($mobile) $mobile = substr_replace($mobile, '****', 3, 4);
        return $mobile;
    }

    public static function convert($num)
    {
        $num = round($num);
        if ($num >= 100000000) {
            $num = round($num / 100000000, 2) . '亿';
        } else if ($num >= 10000) {
            $num = round($num / 10000, 2) . '万';
        }
        // else if ($num >= 1000) {
        //     $num = round($num / 1000, 2) . 'k';
        // }
        return $num;
    }

    public static function shorteStr($str, $length = 48)
    {
        if (mb_strlen($str) > $length) {
            return mb_substr($str, 0, $length) . '...';
        } else {
            return $str;
        }
    }

    public static function generateRandomCode($length = 12, $type = 0)
    {
        $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        if ($type == 1) $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        $code = '';
        for ($i = 0; $i < $length; $i++) {
            $code .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $code;
    }

    public static function getSn($type)
    {
        switch ($type) {
            case 1:
                $str = 'XX' . substr(self::msecTime() . rand(0, 9), 1);
                break;
            case 2:
                $str = 'XXG' . substr(self::msecTime() . rand(0, 5), 1);
                break;
            case 3:
                $str = 'XXP' . substr(self::msecTime() . rand(0, 5), 1);
                break;
            case 4:
                $str = 'XXT' . substr(self::msecTime() . rand(0, 9), 1);
                break;
            case 5:
                $chars = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '2', '3', '4', '5', '6', '7', '8', '9'];
                $charsLen = count($chars) - 1;
                shuffle($chars);
                $str = '';
                for ($i = 0; $i < 6; $i++) {
                    $str .= $chars[mt_rand(0, $charsLen)];
                }
                break;
            case 6:
                $str = 'XXA' . substr(self::msecTime() . rand(0, 9), 1);
                break;
            case 7:
                $str = 'XXAT' . substr(self::msecTime() . rand(0, 9), 1);
                break;
            default:
                $str = substr(self::msecTime() . rand(0, 9), 1);
        }
        return $str;
    }

    public static function msecTime()
    {
        list($tmp1, $tmp2) = explode(' ', microtime());
        return sprintf('%.0f', (floatval($tmp1) + floatval($tmp2)) * 1000);
    }

    public static function calculateOnGrowth($current, $previous)
    {
        if ($previous == 0) return 0;
        $growth = (($current - $previous) / $previous) * 100;
        return round($growth, 2);
    }

    public static function removeEmojis($string)
    {
        $regex = '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}\x{1F900}-\x{1F9FF}\x{FE00}-\x{FEFF}\x{200D}\x{E0020}-\x{E007F}\x{E0100}-\x{E01EF}]/u';
        $cleanedString = preg_replace($regex, '', $string);
        return $cleanedString;
    }
    public static function formatPrice($price)
    {
        $formattedPrice = number_format($price, 2, '.', '');
        return rtrim(rtrim($formattedPrice, '0'), '.');
    }
}
